(function(angular){
      'use strict';
     var app = angular.module('myTodoMVCApp',[]);
     app.controller('Maincontroller', ['$scope','$location', function($scope,$location){
     	$scope.text="";
     	$scope.todos=[
     	    {
     	    	id:1,
               text:"吃饭",
               completed:false
     	    },{
               id:2,
               text:"睡觉",
               completed:true
              },{
               id:3,
               text:"打代码",
               completed:true
              }];
              //显示激活条目
            $scope.todoCount = 0;
            for (var i = 0; i < $scope.todos.length; i++) {
                if (!$scope.todos[i].completed) {
                  $scope.todoCount++;
                }
            };
          //重新激活条目
             $scope.setTodoCount= function(){
               $scope.todoCount = 0;
               for (var i = 0; i < $scope.todos.length; i++) {
                if (!$scope.todos[i].completed) {
                  $scope.todoCount++;
                   console.log($scope.todos[i].completed)
                }
              
            }
            }; 
          //添加
         /*var todoId = 3;
          $scope.add = function($event){
             if ($scope.text.trim().length===0){
               return;
             }
               if (13===event.keyCode){                
                   var obj= {
                      id:++todoId,
                      text:$scope.text,
                      completed:false
                   }
                   $scope.todos.push(obj);
                   $scope.text="";
                   $scope.setTodoCount();
               }
          };*/
           //添加的简单方法 简直坑爹
           var todoId = 3;
          $scope.add = function(){
             if ($scope.text.trim().length===0){
               return;
             }            
                   var obj= {
                      id:++todoId,
                      text:$scope.text,
                      completed:false
                   }
                   $scope.todos.push(obj);
                   $scope.text="";
                   $scope.setTodoCount();               
          };
          //删除
          $scope.remove = function(id){
               for (var i = 0; i < $scope.todos.length; i++) {
                  if ($scope.todos[i].id===id){
                    $scope.todos.splice(i,1);
                  } 
               }
               $scope.setTodoCount();   
          }
          //判断 是否存在已完成的
          $scope.existCompleted = function(){
               for (var i = 0; i < $scope.todos.length; i++) {
                   if ($scope.todos[i].completed){
                    return true;
                   }
               }
          return false;
          }
        //清除已完成项目
        $scope.clearAll = function(){
          var temp = [];
          for (var i = 0; i <$scope.todos.length; i++) {
              if(!$scope.todos[i].completed){
                  temp.push($scope.todos[i]);
               }
          };
          $scope.todos = temp;
          $scope.setTodoCount();  
          /*for (var i = 0; i <$scope.todos.length; i++) {
               if($scope.todos[i].completed){
                   $scope.todos.splice(i,1)
               }
          }*/啃爹
        }; 
          //设置可编辑
          $scope.currentId = -1;
          $scope.setEditing = function(id){
              $scope.currentId=id;
          };
          //按下回车保存
          $scope.save = function(){
                $scope.currentId = -1;
          };
          //全选
          var now = true;
          $scope.toggleAll = function(){
              for (var i = 0  ; i <$scope.todos.length; i++) {
                   $scope.todos[i].completed = now;
              };
              now = !now;
              $scope.setTodoCount();   
          }
      //切换选项卡 
      //  使用过滤器 
       $scope.selector = "";
       $scope.$location = $location;
       $scope.$watch("$location.path()",function(now,old){
         /* console.log(now+"+++++"+old);*/
          switch(now){
          case "/active":
          {
            $scope.selector = false;
            break;
          }
          case "/completed":
          {  
               $scope.selector = true;
             break;  
          }
          default:{
               $scope.selector = '';
             break;  
          }
       }
       });
     }]);     
})(angular);